package javazoom.jlGui.playlist;

/**
 * BasePlaylist.
 *
 *-----------------------------------------------------------------------
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

import java.util.Collection;
import java.util.Vector;
import java.io.*;
import javazoom.Util.*;

/**
 * BasePlaylist.
 * This class implements Playlist interface using a Vector.
 *
 * @author	E.B from JavaZOOM
 *
 * Homepage : http://www.javazoom.net
 */
public class BasePlaylist implements Playlist
{
	protected Vector				_playlist = null;
	protected int   	            _cursorPos = -1;

	/**
	 * Constructor.
	 */
	public BasePlaylist()
	{
		_playlist = new Vector();
	}

	/**
	 * Loads playlist as M3U format.
	 */
	public void load(String filename)
	{
		BufferedReader br = null;
		try
		{
			br = new BufferedReader(new FileReader(filename));
			String line = null;
			String songName = null;
			String songFile = null;
			String songLenght = null;
			while ((line = br.readLine())!=null)
			{
				if (line.startsWith("#"))
				{
					int indA = line.indexOf(",",0);
					if (indA != -1) songName = line.substring(indA+1,line.length());
				}
				else
				{
					boolean isFile = true;
					songFile = line;
					if (songFile.startsWith("http://"))
					{
						isFile = false;
						if (songName == null) songName = songFile;
					}
					if (songName == null)
					{
						songName = songFile;
						songName = songName.substring((songName.lastIndexOf(System.getProperty("file.separator")))+1,songName.length());
					}
					PlaylistItem pli = new PlaylistItem(songName, songFile, -1, isFile);
					this.appendItem(pli);
					songFile = null;
					songName = null;
					songLenght = null;
				}
			}
		} catch (Exception e)
		  {
			trace(1,getClass().getName(),"Can't load playlist : "+e.getMessage());
		  }
		  finally
		  {
			  try
			  {
				  if (br != null) br.close();
			  } catch (Exception ioe)
			    {
					trace(1,getClass().getName(),"Can't close playlist : "+ioe.getMessage());
				}
	  	  }
	}

	/**
	 * Saves playlist.
	 */
	public void save(String filename)
	{
		// TODO
	}

	/**
	 * Adds item at a given position in the playlist.
	 */
	public void addItemAt(PlaylistItem pli, int pos)
	{
		_playlist.insertElementAt(pli,pos);
	}

	/**
	 * Searchs and removes item from the playlist.
	 */
	public void removeItem(PlaylistItem pli)
	{
		_playlist.remove(pli);
	}

	/**
	 * Removes item at a given position from the playlist.
	 */
	public void removeItemAt(int pos)
	{
		_playlist.removeElementAt(pos);
	}

	/**
	 * Append item at the end of the playlist.
	 */
	public void appendItem(PlaylistItem pli)
	{
		_playlist.addElement(pli);
	}

	/**
	 * Sorts items of the playlist.
	 */
	public void sortItems(int sortmode)
	{
		// TODO
	}

	/**
	 * Shuffles items in the playlist randomly
	 */
	public void shuffle()
	{
		// TODO
	}

	/**
	 * Returns item at a given position from the playlist.
	 */
	public PlaylistItem getItemAt(int pos)
	{
		PlaylistItem pli = null;
		pli = (PlaylistItem) _playlist.elementAt(pos);
		return pli;
	}

	/**
	 * Returns a collection of playlist items.
	 */
	public Collection getAllItems()
	{
		// TODO
		return null;
	}

	/**
	 * Returns then number of items in the playlist.
	 */
	public int getPlaylistSize()
	{
		return _playlist.size();
	}

    // Next methods will be used by the Player

	/**
	 * Returns item matching to the cursor.
	 */
	public PlaylistItem getCursor()
	{
		if((_cursorPos < 0) || (_cursorPos >= _playlist.size())) return null;
		return getItemAt(_cursorPos);
	}

	/**
	 * Computes cursor position (next).
	 */
	public void nextCursor()
	{
		_cursorPos++;
		if (_cursorPos >= _playlist.size()) _cursorPos = _playlist.size();
	}

	/**
	 * Computes cursor position (previous).
	 */
	public void previousCursor()
	{
		_cursorPos--;
		if(_cursorPos < 0) _cursorPos = -1;
	}

	/**
	 * Sends traces to Debug.
	 */
	private void trace(int level, String msg1, String msg2)
	{
		Debug dbg = Debug.getInstance();
		dbg.log(level,msg1+":"+msg2);
	}
}